package com.xiaomili.halfsugar.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.squareup.picasso.Picasso;
import com.xiaomili.halfsugar.CircleImageView;
import com.xiaomili.halfsugar.R;
import com.xiaomili.halfsugar.adapter.ListDetailRecyclerViewAdapter;
import com.xiaomili.halfsugar.bean.ListDetailBean;
import com.xiaomili.halfsugar.constant.UrlString;
import com.xiaomili.halfsugar.utils.OkHttpUtil;

import java.io.IOException;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * A simple {@link Fragment} subclass.
 */
public class ListDetailFragment extends Fragment {


    @InjectView(R.id.recycler)
    XRecyclerView mRecycler;
    @InjectView(R.id.iv_pic)
    ImageView mIvPic;

    private static final String KEY = "param";
    private ListDetailRecyclerViewAdapter mAdapter;
    private ListDetailBean.DataBean mData;
    private View mHeader;

    public static ListDetailFragment newInstance(String id) {

        Bundle args = new Bundle();
        args.putString(KEY, id);
        ListDetailFragment fragment = new ListDetailFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_list_detail, container, false);
        mHeader = LayoutInflater.from(getActivity()).inflate(R.layout.recyclerview_header,container,false);
        ButterKnife.inject(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        if (getArguments() != null) {
            String id = getArguments().getString(KEY);
            OkHttpUtil.newInstance(getActivity()).getAsyncData(UrlString.LISTDETAIL_URL + id, new OkHttpUtil.MyCallBack() {
                @Override
                public void onFailure(IOException e) {

                }

                @Override
                public void onResponse(String result) {
                    Gson gson = new Gson();
                    ListDetailBean listDetailBean = gson.fromJson(result, ListDetailBean.class);
                    TextView tvTitle = (TextView) mHeader.findViewById(R.id.tv_title);
                    CircleImageView civ = (CircleImageView) mHeader.findViewById(R.id.civ_icon);
                    TextView tvName = (TextView) mHeader.findViewById(R.id.tv_name);
                    TextView tvNumber = (TextView) mHeader.findViewById(R.id.tv_number);
                    TextView tvDesc = (TextView) mHeader.findViewById(R.id.tv_desc);
                    Picasso.with(getActivity()).load(listDetailBean.getData().getPic()).into(mIvPic);
                    tvTitle.setText(listDetailBean.getData().getTitle());
                    Picasso.with(getActivity()).load(listDetailBean.getData().getUser().getAvatar()).into(civ);
                    tvName.setText(listDetailBean.getData().getUser().getNickname());
                    tvNumber.setText(listDetailBean.getData().getViews());
                    tvDesc.setText(listDetailBean.getData().getDesc());
                    mData = listDetailBean.getData();
                    mAdapter = new ListDetailRecyclerViewAdapter(getActivity(), mData);
                    mRecycler.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
                    mRecycler.addHeaderView(mHeader);
                    mRecycler.setPullRefreshEnabled(false);
                    mRecycler.setAdapter(mAdapter);
                }
            });
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
